<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="租户名称" prop="tenantName">
        <el-input v-model="queryParams.tenantName" placeholder="请输入租户名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="租户编码" prop="tenantCode">
        <el-input v-model="queryParams.tenantCode" placeholder="请输入租户编码" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="网站域名" prop="webDomain">
        <el-input v-model="queryParams.webDomain" placeholder="请输入网站域名" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:tenant-info:create']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="noticeList">
      <el-table-column label="序号" type="index" width="100" align="center" :show-overflow-tooltip="true">
        <template scope="scope">
          <span>{{scope.$index + 1}}</span>
        </template>
      </el-table-column>
      <el-table-column label="租户编码" align="center" prop="tenantCode" width="200" />
      <el-table-column label="租户名称" align="left" prop="tenantName" :show-overflow-tooltip="true" />
      <el-table-column label="网站域名" align="center" prop="webDomain" :show-overflow-tooltip="true" />
      <el-table-column label="后台域名" align="center" prop="adminDomain" :show-overflow-tooltip="true" />
      <el-table-column label="备注" align="left" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:tenant-info:update']">修改</el-button>
          <el-button size="mini" type="text" @click="sendMsg(scope.row)" v-hasPermi="['system:tenant-info:sms']">短信</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <el-dialog v-dialogDrag title="短信配置" :visible.sync="sendsms" width="1300px" top="25vh">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddSMS" v-hasPermi="['system:tenant-sms:create']">新增</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="innerList">
        <el-table-column label="序号" type="index" width="100" align="center">
          <template scope="scope">
            <span>{{scope.$index + 1}}</span>
          </template>
        </el-table-column>

        <el-table-column label="是否扣减" align="center" prop="isDeduct" width="">
          <template slot-scope="scope">
            <span>{{ scope.row.isDeduct?'是':'否' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="每日最大次数" align="center" prop="limitNumDay" width="" />
        <el-table-column label="短信码过期（秒）" align="center" prop="smsCodeExpire" width="" />
        <el-table-column label="短信码长度" align="center" prop="smsCodeLen" width="" />
        <el-table-column label="短信剩余数量" align="center" prop="smsSurplusNum" width="" />
        <!--<el-table-column label="短信总计数量" align="center" prop="smsTotalNum" width="" />-->
        <el-table-column label="状态" align="center" prop="status" width="">
          <template slot-scope="scope">
            <span>{{ scope.row.status?'正常':'禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateInner(scope.row)" v-hasPermi="['system:tenant-sms:update']">修改
            </el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:tenant-sms:delete']">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="sendsms = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改对话框 -->
    <el-dialog v-dialogDrag :title="titleInner" :visible.sync="smsInneropen" width="900px" top="25vh" append-to-body>
      <el-form ref="innerform" :model="innerform" label-width="160px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="日限流数量" prop="limitNumDay">
              <el-input v-model="innerform.limitNumDay" placeholder="请输入日限流数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否扣减" prop="isDeduct">
              <el-radio-group v-model="innerform.isDeduct">
                <el-radio :label="true">是</el-radio>
                <el-radio :label="false">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="验证码过期时间（秒）" prop="smsCodeExpire">
              <el-input v-model="innerform.smsCodeExpire" placeholder="请输入验证码过期时间" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="验证码长度" prop="smsCodeLen">
              <el-input v-model="innerform.smsCodeLen" placeholder="请输入验证码长度" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="短信剩余数量" prop="smsSurplusNum">
              <el-input v-model="innerform.smsSurplusNum" placeholder="请输入短信剩余数量" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="innerform.status">
                <el-radio :label="0">禁用</el-radio>
                <el-radio :label="1">正常</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="短信渠道" prop="channelId">
              <el-select v-model="innerform.channelId" placeholder="请选择" style="width:100%">
                <el-option v-for="dict in smsList" :key="parseInt(dict.id)" :label="dict.code" :value="parseInt(dict.id)" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitInnerForm">确 定</el-button>
        <el-button @click="cancelInner">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改对话框 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="780px" top="25vh" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="租户编码" prop="tenantCode">
              <el-input v-model="form.tenantCode" placeholder="请输入租户编码" maxlength="50" v-if="form.id!=null" :disabled=true />
              <el-input v-model="form.tenantCode" placeholder="请输入租户编码" maxlength="50" v-if="form.id==null || form.id==''" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="租户名称" prop="tenantName">
              <el-input v-model="form.tenantName" placeholder="请输入租户名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="后台域名" prop="adminDomain">
              <el-input v-model="form.adminDomain" placeholder="请输入后台域名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="网站域名" prop="webDomain">
              <el-input v-model="form.webDomain" placeholder="请输入网站域名" />
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="title === '添加租户信息'">
            <el-form-item label="短信渠道" prop="smsChannelId">
              <el-select v-model="form.smsChannelId" placeholder="请选择" style="width:100%">
                <el-option v-for="dict in smsList" :key="parseInt(dict.id)" :label="dict.code" :value="parseInt(dict.id)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="租户类型" prop="tenantTypeId">
              <el-select v-model="form.tenantTypeId" placeholder="请选择" style="width:100%">
                <el-option v-for="dict in typeList" :key="parseInt(dict.id)" :label="dict.tenantTypeName" :value="parseInt(dict.id)" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { createTenantInfo, getTenantInfo, getTenantInfoPage, updateTenantInfo, createTenantSMS, delTenantSMS, getTenantSMS, getTenantSMSPage, updateTenantSMS } from "@/api/system/tenantInfo";
import { getSimpleSmsChannels } from '@/api/system/sms/smsChannel';
import { getAllTenantType } from "@/api/system/tenantType";

export default {
  name: "Application",
  components: {
  },
  data() {
    return {
      smsInneropen: false,
      sendsms: false,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      noticeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 20,
      },
      rowItem: {},
      titleInner: '',
      smsList: [],
      typeList: [],
      innerList: [],
      // 表单参数
      form: {},
      innerform: {},
      // 表单校验
      rules: {
        tenantCode: [
          { required: true, message: "租户编码不能为空", trigger: "blur" }
        ],
        tenantName: [
          { required: true, message: "租户名称不能为空", trigger: "blur" }
        ],
        tenantTypeId: [
          { required: true, message: "请选择租户类型", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
    this.getSMS()
    this.getType()

  },
  methods: {
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      getTenantInfoPage(this.queryParams).then(response => {
        this.noticeList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getInnerList() {
      getTenantSMSPage({ tenantCode: this.rowItem.tenantCode }).then(response => {
        this.innerList = response.data.list;
        this.sendsms = true
      });
    },
    getType() {
      getAllTenantType().then(res => {
        this.typeList = res.data
      })
    },
    getSMS() {
      getSimpleSmsChannels().then(res => {
        this.smsList = res.data
      })
    },
    cancelInner() {
      this.smsInneropen = false
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    sendMsg(row) {
      this.rowItem = row
      this.getInnerList()
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加租户信息";
    },

    handleAddSMS() {
      this.smsInneropen = true
      this.titleInner = "添加租户短信配置";
      this.innerform = {}
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTenantInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改租户信息";
      });
    },
    handleUpdateInner(row) {
      this.reset();
      getTenantSMS(row.id).then(response => {
        this.innerform = response.data;
        this.smsInneropen = true;
        this.titleInner = "修改租户短信配置";
      });
    },
    submitInnerForm: function () {
      this.innerform.tenantCode = this.rowItem.tenantCode
      if (this.innerform.id !== undefined) {
        updateTenantSMS(this.innerform).then(response => {
          this.msgSuccess("修改成功");
          this.smsInneropen = false;
          this.getInnerList();
        });
      } else {
        createTenantSMS(this.innerform).then(response => {
          this.msgSuccess("新增成功");
          this.smsInneropen = false;
          this.getInnerList();
        });
      }

    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id !== undefined) {
            updateTenantInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            createTenantInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id
      this.$confirm('是否确认删除数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delTenantSMS(ids);
      }).then(() => {
        this.getInnerList();
        this.msgSuccess("删除成功");
      })
    }
  }
};
</script>
